<?php
require_once(dirname(__FILE__)."/../libs/string/cart.php");
require_once(dirname(__FILE__)."/../config/session.php");
require_once(dirname(__FILE__)."/../config/config.inc.php");
require_once(dirname(__FILE__)."/../libs/string/libstr.php");
require_once(dirname(__FILE__)."/../libs/dbclass/dbconn.php");
require_once(dirname(__FILE__)."/../libs/dbclass/dbservice.php");
require_once(dirname(__FILE__)."/../libs/login/login.php");
require_once(dirname(__FILE__)."/../libs/string/cartservice.php");
$mc=new libstr();
$db=new dbconn();
$dbs=new dbservice();
$lp=new login();

if($mc->Ismp($_POST["addcart"])!="")
{
	if($mc->Ismp($lp->getsession("userid"))!="")
	{
		if($lp->getsession("gyid")!="0")
		{
			echo "pk";
			exit();
		}
	}
	$items1 = $cart->getCart();
	foreach($items1 as $rs)
	{
		if(trim($rs["codeid"])==trim($mc->request("codeid","")))
		{
			echo "no";
			exit();
		}
	}
	srand((double)microtime()*1000000);
	$randval = substr(rand(), 0,4);
	$id=date("YmdHis").$randval;

	//-----------
	if($_POST["tp"]=="0")
	{
		$myrs1=$db->dvstr("select count(id) as nok from product_content where price_free=0 and id ='".$_POST["id"]."'");
		if($myrs1["nok"]==0)
		{
			echo "np";
			exit();
		}
		$myrs=$db->dvstr("select * from product_content where id ='".$_POST["id"]."'");
		$cdrs=$db->dvstr("select count(id) as ok  from xinghao_content where pid>0 and codeid ='".$myrs["codeid"]."'");
		if($cdrs["ok"]==0)
		{
			echo "databad";
			exit();
		}
		$xinghaotxt="";
		$xinghaoval="";
		if($mc->Ismp($_POST["xinghao"])!="")
		{
			$myrsbh=$db->dvstr("select * from xinghao_content where id ='".$_POST["xinghao"]."'");
			$xinghaotxt="(".trim($myrsbh["x_text"]).")";
			$xinghaoval=$myrsbh["id"];
		}


		$cartitems=array();
		$cartitems[0]=$id;//ID
		$cartitems[1]=trim($myrs['codeid']);//codeid
		$cartitems[2]=trim($myrs['topicname']).$xinghaotx;//name
		$cartitems[3]=trim($myrs['price_s']);//price_s
		$cartitems[4]=trim($myrs['price_m']);//price_y
		$cartitems[5]=$mc->request("num","1");//num
		$cartitems[6]="0";//leixing
		$cartitems[7]=trim($myrs['id']);//url
		$cartitems[8]=trim($myrs['jifen']);//jifen
		$cartitems[9]=trim($myrs['zhongliang']);//zhongliang
		$cartitems[10]=$mc->getpics(trim($myrs['pic']),0);
		$cartitems[11]=trim($myrs['gyid']);
		$cartitems[12]=$xinghaotxt;//a1
		$cartitems[13]=$xinghaoval;//a2
		$cartitems[14]="";
		$cartitems[15]="";
		$items=$cart->add($cartitems);
	}
	//-----------
	$_SESSION["cartnum"]=0;
	foreach($items as $rs)
	{
		if(trim($rs["codeid"])==trim($mc->request("codeid","")))
		{
			$_SESSION["cartnum"]=$_SESSION["cartnum"]+$rs["num"];
		}
	}
	echo "ok";
}
if($mc->Ismp($_GET["editcart"])!="")
{
	$key = $_POST['key'];

	$value = $_POST['num'];
	for($i=0;$i<count($key);$i++){
		if($mc->isnum($value[$i])){
			$items = $cart->modi($key[$i],"num",$value[$i]);
		}
	}
	$_SESSION["cartnum"]=0;
	foreach($items as $rs)
	{
		if(trim($rs["codeid"])==trim($mc->request("codeid","")))
		{
			$_SESSION["cartnum"]=$_SESSION["cartnum"]+$rs["num"];
		}
	}
	if($mc->Ismp($_GET["net"])=="true")
	{
		$mc->msg("","../netshop/cart_list.php");
	}
	if($mc->Ismp($_GET["to"])!="")
	{
		$mc->msg("","../".$_GET["to"]."/cart_list.php");
	}
	$mc->msg("","../cart_list.php");
}
if($mc->Ismp($_GET["deletecart"])!="")
{
	$items = $cart->remove($_GET['deletecart']);
	$_SESSION["cartnum"]=0;
	foreach($items as $rs)
	{
		if(trim($rs["codeid"])==trim($mc->request("codeid","")))
		{
			$_SESSION["cartnum"]=$_SESSION["cartnum"]+$rs["num"];
		}
	}
		if($mc->Ismp($_GET["net"])=="true")
	{
		$mc->msg("","../netshop/cart_list.php");
	}
		if($mc->Ismp($_GET["to"])!="")
	{
		$mc->msg("","../".$_GET["to"]."/cart_list.php");
	}
	$mc->msg("","../cart_list.php");
}
if($mc->Ismp($_GET["deletecartall"])!="")
{
	$_SESSION["cartnum"]=0;
	session_unregister("cart");
		if($mc->Ismp($_GET["net"])=="true")
	{
		$mc->msg("","../netshop/cart_list.php");
	}
		if($mc->Ismp($_GET["to"])!="")
	{
		$mc->msg("","../".$_GET["to"]."/cart_list.php");
	}
	$mc->msg("","../cart_list.php");
}

if($mc->Ismp($_GET["savecart"])!="")
{
	$clist=new cartservice();
	$clrs=$clist->getcart();
	if($clist->zongshu<1)
	{
		$mc->msg("购物车为空，无法下订单。","go");
	}
	if($_POST["zhifufangshi"]=="huokuan")
	{
	$grs=$db->dvstr("select * from member_content where userno=".$lp->getsession("userno")."");


		if($_POST["zongprice"]>$grs["tjinqian"])
		{
           $mc->msg("您的账户余额不足，无法下订单，请先充值。","go");
		}
	}
	$dtime=date("Y-m-d H:i:s");
	srand((double)microtime()*1000000);
	$randval = substr(rand(), 0,4);
	$_SESSION['orderid']=date("YmdHis").$randval;
	//	print_r($_POST);
	//exit();
	//------------------
	$_userno=$lp->getsession("userno");
	$_username=$mc->request("username","");
	$_userid=$lp->getsession("userid");
	$_sex=$mc->request("sex","0");

	if($_sex=="0")
	{
		$_username.=" 先生";
	}else
	{
		$_username.=" 女士";
	}
	$_dingdanhao=$_SESSION['orderid'];
	$_creattime=$dtime;
	$_email=$mc->request("email","");
	$_qq=$mc->request("qq","");
	$_shouji=$mc->request("shouji","");
	$_tel=$mc->request("tel","");
	$_dizhi=$mc->request("dizhi","");
	$_youbian=$mc->request("youbian","");
	$_content=$mc->request("content","");
	$_peisong=$mc->request("peisong","");
	$_peisongprice=$mc->request("peisongprice","0");
	$_zhifufangshi=$mc->request("zhifufangshi","");
	$_zhifuprice=$mc->request("zhifuprice","0");
	$_fapiao=$mc->request("fapiao","");
	$_fapiaoprice=$mc->request("fapiaoprice","0");
	$_fapiaotaitou=$mc->request("fapiaotaitou","");
	$_jifen=$mc->request("jifen","0");
	$_jifenstat=0;
	$_zhuangtai="0";
	$_zhongliang=$mc->request("zhongliang","0");
	$_chengshi=$mc->request("chengshi","");
	$_zongjia=$mc->request("zongprice","0");
	$_isshow=$mc->request("isshow","0");
	$_updatetime=$mc->request("updatetime",date("Y-m-d H:i:s"));
	$_paytime=$mc->request("paytime","1980-06-10 15:50:2");

    $xk="2";
    $xb="1";
	if($_POST["zhifufangshi"]=="huokuan")
	{
	$xk="6";
    $xb="5";
    if($lp->getsession("rootid")=='0,5,')
	{
	  $xk="2";
      $xb="1";
	}
    $dingid=$_dingdanhao;
	$_x_text="消费";
	$_x_price=$_zongjia;
	$_userno=$lp->getsession("userno");
	$_x_stat="ok";
	$_zt=1;
	$_lx=1;
	$sql="insert into jiaoyi_content(x_text,x_price,x_stat,zt,createtime,lx,dingdanhao,userno)values('".$_x_text."',".$_x_price.",'".$_x_stat."','".$_zt."','".date("Y-m-d H:i:s")."',".$_lx.",".$dingid.",".$_userno.")";
	//echo $sql;
	//exit();
	$db->cmd($sql);
	$db->cmd("update member_content set tjinqian=tjinqian-".$_x_price.",xjinqian=xjinqian+".$_x_price." where userno=".$lp->getsession("userno")."");
    
	}
	$_rootid=$dbs->getrootdata("order_class",$xk);
	$_rootid1=$dbs->getrootdata("zhifu_class",$xb);
	$_chuhuotime=$mc->request("chuhuotime","1980-06-10 15:50:2");
	$_ems=$mc->request("ems","");
	$_beizhu="";
	$_gyid="0";
	$ordersql="insert into order_content(gyid,userno,username,userid,sex,dingdanhao,creattime,email,qq,shouji,tel,dizhi,youbian,content,peisong,peisongprice,zhifufangshi,zhifuprice,fapiao,fapiaoprice,fapiaotaitou,jifen,jifenstat,zhuangtai,zhongliang,chengshi,zongjia,rootid,isshow,updatetime,paytime,rootid1,chuhuotime,ems,beizhu)values(".$_gyid.",".$_userno.",'".$_username."','".$_userid."',".$_sex.",'".$_dingdanhao."','".$_creattime."','".$_email."','".$_qq."','".$_shouji."','".$_tel."','".$_dizhi."','".$_youbian."','".$_content."','".$_peisong."',".$_peisongprice.",'".$_zhifufangshi."',".$_zhifuprice.",'".$_fapiao."',".$_fapiaoprice.",'".$_fapiaotaitou."',".$_jifen.",".$_jifenstat.",".$_zhuangtai.",".$_zhongliang.",'".$_chengshi."',".$_zongjia.",'".$_rootid."',".$_isshow.",'".$_updatetime."','".$_paytime."','".$_rootid1."','".$_chuhuotime."','".$_ems."','".$_beizhu."')";
	//echo $ordersql;
	//exit();
	$db->cmd($ordersql);

	//------------------
	foreach ($clrs as $rs)
	{
		$_topicname=trim($rs["name"]);
		$_codeid=trim($rs["codeid"]);
		$_url=trim($rs["url"]);
		$_dingdanhao=trim($_SESSION['orderid']);
		$_creattime=trim($dtime);
		$_price_m=trim($rs["price_s"]);
		$_price_s=trim($rs["price_s"]);
		$_price_v=trim($rs["price_y"]);
		$_num=trim($rs["num"]);
		$_zhongliang=trim($rs["zhongliang"]);
		$_jifen=trim($rs["jifen"]);

		$_price_y=trim($rs["price_y"]);
		$_updatetime=$dtime;
		$_zhuangtai="1";
		if($_POST["zhifufangshi"]=="huokuan")
	    {
			$_zhuangtai="2";
		}
			
	if($lp->getsession("rootid")=='0,5,')
	{
	 	$_zhuangtai="3";
	}
		$_gyid=trim($rs["gyid"]);
		$_xishu="0";
		$getxishu=$db->dvstr("select xishu from gongyingshang_content where gyid=".$_gyid."");
		if($getxishu["xishu"]!="")
		{
			$_xishu=$getxishu["xishu"];
		}
		$db->cmd("insert into cart_content(xishu,gyid,userno,topicname,codeid,url,dingdanhao,creattime,price_m,price_s,price_v,num,zhongliang,jifen,userid,price_y,updatetime,zhuangtai,a1,a2)values(".$_xishu.",".$_gyid.",".$_userno.",'".$_topicname."','".$_codeid."','".$_url."','".$_dingdanhao."','".$_creattime."',".$_price_m.",".$_price_s.",".$_price_v.",".$_num.",".$_zhongliang.",".$_jifen.",'".$_userid."',".$_price_y.",'".$_updatetime."',".$_zhuangtai.",'".trim($rs["a1"])."','".trim($rs["a2"])."')");

	}
	//----------------------

	$_SESSION["orderpost"]=$_POST;
	$_SESSION["orderpost"]["riqi"]=$dtime;
	$_SESSION["orderpost"]["dingdanhao"]=$_dingdanhao;
		if($mc->Ismp($_GET["net"])=="true")
	{
		$mc->msg("","../netshop/cart_ok.php");
	}
		if($mc->Ismp($_GET["to"])!="")
	{
		$mc->msg("","../".$_GET["to"]."/cart_ok.php");
	}
	$mc->msg("","../cart_ok.php");
}
if($mc->Ismp($_POST["shoucang"])!="")
{
	if($mc->Ismp($lp->getsession("userno"))=="")
	{
		echo "0";
		exit();
	}
	else{

		if($lp->getsession("gyid")!="0")
		{
			echo "pk";
			exit();
		}
	}
	$_proid=$mc->request("proid","0");
	$_codeid=$mc->request("codeid","");
	$_userno=$lp->getsession("userno");
	$_createtime=$mc->request("createtime",date("Y-m-d H:i:s"));
	if($_proid=="" || $_codeid==""  )
	{
		echo "1";
		exit();
	}
	$getrs=$db->dvstr("select count(id) as gno from product_content where id=".$_proid." and isshow>-1");
	if($getrs["gno"]==0)
	{
		echo "2";
		exit();
	}
	$getrs1=$db->dvstr("select count(id) as gno from shoucang_content where userno=".$_userno." and proid=".$_proid."");
	if($getrs1["gno"]!=0)
	{
		echo "3";
		exit();
	}

	$db->cmd("insert into shoucang_content(proid,codeid,userno,createtime)values(".$_proid.",'".$_codeid."',".$_userno.",'".$_createtime."')");
	echo "ok";
	exit();
}
if($mc->Ismp($_GET["scdelid"])!="")
{
	$sql="delete from shoucang_content where proid =".$_GET['scdelid']." and userno=".$lp->getsession("userno")."";
	//echo $sql;
	//exit();
	$db->cmd($sql);
	$mc->msg("","../uc_shoucang.php");
}
//$items = $cart->getCart();
?>